package com.sky.service.impl;

import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
@Autowired
private ReportMapper reportMapper;

@Autowired
private UserMapper userMapper;
@Autowired
private OrderMapper orderMapper;

    @Override
    public TurnoverReportVO turnover(LocalDate begin, LocalDate end) {

        List<LocalDate>  localDates=new ArrayList<>();
        localDates.add(begin);

        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            localDates.add(begin);
        }
        String dateList = StringUtils.join(localDates, ",");
        List<Double>  dailyMoney=new ArrayList<>();
        for (LocalDate localDate : localDates) {
            LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime dead = LocalDateTime.of(localDate, LocalTime.MAX);
           Double  sum= reportMapper.sum(start,dead);
           sum=(sum==null)?0.0:sum;
           dailyMoney.add(sum);
        }
        String turnoverList = StringUtils.join(dailyMoney, ",");


        return TurnoverReportVO
                .builder()
                .dateList(dateList)
                .turnoverList(turnoverList)
                .build();
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate>  localDates=new ArrayList<>();
        localDates.add(begin);

        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            localDates.add(begin);
        }
        String dateList = StringUtils.join(localDates, ",");


        List<Integer>  newUser=new ArrayList<>();
        List<Integer>  total=new ArrayList<>();
        for (LocalDate localDate : localDates) {
            LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime dead = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer  sum= userMapper.sumUser(start,dead);
            sum=(sum==null)?0:sum;
            newUser.add(sum);
            Integer sum1 = userMapper.sum(dead);
            sum1=(sum1==null)?0:sum1;
            total.add(sum1);
        }

        String newUserList = StringUtils.join(newUser,",");

        String totalUserList = StringUtils.join(total, ",");
        return UserReportVO.builder()
                .dateList(dateList)
                .newUserList(newUserList)
                .totalUserList(totalUserList)
                .build();
    }

    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate>  localDates=new ArrayList<>();
        localDates.add(begin);

        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            localDates.add(begin);
        }
        String dateList = StringUtils.join(localDates, ",");

        List<Integer>  validOrderCountList=new ArrayList<>();

        List<Integer>  orderCountList=new ArrayList<>();
        for (LocalDate localDate : localDates) {
            LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime dead = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer sum= orderMapper.validOrderCountList(start,dead);
            sum=(sum==null)?0:sum;
            validOrderCountList.add(sum);

            Integer sum1 = orderMapper.orderCountList(start,dead);
            sum1=(sum1==null)?0:sum1;
            orderCountList.add(sum1);
        }

        Integer validOrderCount=validOrderCountList.stream().reduce(Integer::sum).get();
        Integer totalOrderCount=orderCountList.stream().reduce(Integer::sum).get();
        Double orderCompletionRate=0.0;
        if (totalOrderCount!=0) orderCompletionRate=validOrderCount.doubleValue()/totalOrderCount;

        return OrderReportVO.builder()
                .dateList(dateList)
                .validOrderCountList(StringUtils.join(validOrderCountList,","))
                .orderCountList(StringUtils.join(orderCountList,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }
}
